package com.happyfamily.springboot.crowdsourcingplatform.service.wang.impl;

import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dao.wang.StoryDao;
import com.happyfamily.springboot.crowdsourcingplatform.model.UserStory;
import com.happyfamily.springboot.crowdsourcingplatform.service.wang.StoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class StoryServiceImpl implements StoryService
{
	@Autowired
	private StoryDao storyDao;

	//前台展示雇主的故事列表
	@Override
	public List<UserStory> storyList()
	{
		return storyDao.storyList();
	}

	//前台展示更多的雇主故事
	@Override
	public Results<UserStory> findStoryList(Integer startPosition, Integer limit, String who, String starttime, String endtime, String stitle,String storySticky)
	{
		return Results.success(storyDao.findStoryCount(who,starttime,endtime,stitle,storySticky).intValue(),storyDao.findStoryList(startPosition,limit,who,starttime,endtime,stitle,storySticky));
	}

	//前台展示雇主我的故事
	@Override
	public Results<UserStory> myStoryList(Integer startPosition, Integer limit, String starttime, String endtime, String stitle, String storySticky, String storyStatus, String scontent, HttpServletRequest request)
	{
		SecurityContextImpl securityContext=(SecurityContextImpl)request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String uaccount=securityContext.getAuthentication().getName();
		return Results.success(storyDao.myStoryCount(uaccount,starttime,endtime,stitle,storySticky,storyStatus,scontent).intValue(),storyDao.myStoryList(uaccount,startPosition,limit,starttime,endtime,stitle,storySticky,storyStatus,scontent));
	}

	//前台雇主发布故事
	@Override
	public String storyAdd(String titles, String contents, HttpServletRequest request)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String stime=sdf.format(new Date());// new Date()为获取当前系统时间
		SecurityContextImpl securityContext=(SecurityContextImpl)request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String uaccount=securityContext.getAuthentication().getName();
		int index=storyDao.storyAdd(uaccount,titles,contents,stime);
		String res="error";
		if (index>0)
		{
			res="success";
		}
		return res;
	}

	//前台雇主对故事的重新发布
	@Override
	public String resubmit(String sid)
	{
		String res="error";
		int index=storyDao.resubmit(sid);
		if (index>0)
		{
			res="success";
		}
		return res;
	}


	//后台管理员对雇主故事的表格展示
	@Override
	public Results<UserStory> storyTable(Integer startPosition, Integer limit, String who, String starttime, String endtime, String status, String sticky)
	{
		return Results.success(storyDao.storyTableCount(who,starttime,endtime,status,sticky).intValue(),storyDao.storyTable(startPosition,limit,who,starttime,endtime,status,sticky));
	}

	//后台对故事的删除
	@Override
	public String storyDelete(String sid)
	{
		String res="error";
		int index=storyDao.storyDelete(sid);
		if (index>0)
		{
			res="success";
		}
		return res;
	}

	//后台对故事的审核通过
	@Override
	public String storyYes(String sid)
	{
		String res="error";
		int index=storyDao.storyYes(sid);
		if (index>0)
		{
			res="success";
		}
		return res;
	}

	//后台对故事的审核 不通过
	@Override
	public String storyNo(String sid)
	{
		String res="error";
		int index=storyDao.storyNo(sid);
		if (index>0)
		{
			res="success";
		}
		return res;
	}

	//后台对故事的 取消置顶
	@Override
	public String cancelSticky(String sid)
	{
		String res="error";
		int index=storyDao.cancelSticky(sid);
		if (index>0)
		{
			res="success";
		}
		return res;
	}

	//后台对故事的置顶操作
	@Override
	public String doSticky(String sid)
	{
		String res="error";
		int index=storyDao.doSticky(sid);
		if (index>0)
		{
			res="success";
		}
		return res;
	}
}
